package uno.linze.wallpaper.domain.identity.dto;

import cn.hutool.core.util.StrUtil;
import lombok.Data;
import lombok.Getter;
import lombok.ToString;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import uno.linze.wallpaper.common.exception.BusinessException;

import java.util.Objects;

@Data
public class UserRegister {
    private final String username;
    private final String email;
    private final String password;
    private final String verificationCode; // 可选，暂未实现

    public UserRegister(String username, String email, String password,String verificationCode) {
        this.username = username;
        this.email = email;
        this.password = encryptPassword(password);
        this.verificationCode = verificationCode;
        validate();
    }

    // 领域行为：注册信息校验
    public void validate() {
        if (StrUtil.isBlank(username)) {
            throw new BusinessException("用户名不能为空");
        }
        if (StrUtil.isBlank(email) || !email.contains("@")) {
            throw new BusinessException("邮箱格式不正确");
        }
        if (StrUtil.isBlank(password) || password.length() < 6) {
            throw new BusinessException("密码长度不能小于6位");
        }
    }

    // 密码加密
    private String encryptPassword(String rawPassword) {
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        return encoder.encode(rawPassword);
    }

}